<script setup>
import { loginApi } from "@/api/user";
import { User, Lock } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import { ref } from "vue";
import { useRouter } from "vue-router";
import useUserStore from "@/stores/user";
const router = useRouter();
const userStore = useUserStore();

// vue中路由声明式导航router-link与编程式导航router.push()

const loginForm = ref({
  username: "",
  password: "",
});

const rules = {
  username: [
    { required: true, message: "用户名称不能为空", trigger: "blur" },
    {
      pattern: /^[a-zA-Z0-9]{1,10}$/,
      message: "用户名称1~10位英文数字组成",
      trigger: "blur",
    },
  ],
  password: [
    { required: true, message: "用户密码不能为空", trigger: "blur" },
    {
      pattern: /^\S{6,12}$/,
      message: "用户密码6~12位的非空字符",
      trigger: "blur",
    },
  ],
};

const loginFormRef = ref(null);

const login = () => {
  // 1.数据合法性校验 兜底校验
  loginFormRef.value.validate(async (valid) => {
    if (!valid) return;

    // 2.ajax请求登录数据
    const { data: res } = await loginApi(loginForm.value);
    console.log(res);

    // 3.判断登录是否成功
    // 3.1失败
    if (res.code !== 0) return ElMessage.error(res.message);
    // 3.2成功
    ElMessage.success(res.message);

    // 4.将登录成功后用户token存起来
    userStore.updateToken(res.token);

    // 4.登录成功跳转到后台首页
    router.push("/");
  });
};
</script>

<template>
  <!-- 登录页面的整体盒子 -->
  <div class="login-container">
    <!-- 登录的盒子 -->
    <div class="login-box">
      <!-- 标题的盒子 -->
      <div class="title-box"></div>
      <!-- 登录的表单区域  三层结构 四个属性-->
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            :prefix-icon="User"
            placeholder="请输入用户名称"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            :prefix-icon="Lock"
            placeholder="请输入用户密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn-login" @click="login"
            >登录</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-link :underline="false" @click="router.push('/reg')"
            >去注册</el-link
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style scoped lang="less">
.login-container {
  background: url("../../assets/images/login_bg.jpg") center;
  background-size: cover;
  height: 100%;

  .login-box {
    width: 400px;
    height: 270px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    padding: 0 30px;
    box-sizing: border-box;

    .title-box {
      height: 60px;
      background: url("../../assets/images/login_title.png") center no-repeat;
    }

    .el-input {
      height: 40px;
    }

    .btn-login {
      width: 100%;
      height: 40px;
    }
  }
}
</style>
